package com.jiangwh.domain.aop;

import java.text.MessageFormat;
import java.util.Calendar;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class PerformanceLog {
	
	private final static Logger logger= Logger.getLogger("performance");
	
	@Pointcut("execution (public * com.jiangwh.domain.services..*.*(..))")
	public void advice(){}

	@Around("advice()")
	public Object around(ProceedingJoinPoint point) throws Throwable {
		long timeBegin = Calendar.getInstance().getTimeInMillis();
		Object object = point.proceed();		
		long timeCost = Calendar.getInstance().getTimeInMillis() - timeBegin;
		logger.debug(MessageFormat.format(
				"Call AOP Point[{0}],target[target:{1},type:{2},method:{3}] cost time[{4}]ms",
				new Object[] {point, point.getTarget(),
						point.getSignature().getDeclaringTypeName(),
						point.getSignature().getName(), timeCost }));
		return object;
	}

}
